using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.Serialization;

namespace WcfService1.DTO
{
    [DataContract]
    public class CashBankOutDTO : INotifyPropertyChanged
    {
        private CoaDTO _CashBankAccount;
        private ObservableCollection<CashBankOutDetailDTO> _CashBankOutDetails;
        private String _Description;
        private String _NoVoucher;
        private SupplierDTO _Supplier;


        private DateTime _Tanggal;
        private int _No;
        private decimal _Balance;
        private decimal _TotalKredit;
        private decimal _TotalDebit;

        public CashBankOutDTO()
        {
                CashBankOutDetails = new ObservableCollection<CashBankOutDetailDTO>();
                Tanggal = DateTime.Now.Date;
        }

        [DataMember]
        public String NoVoucher
        {
            get { return _NoVoucher; }
            set
            {
                if (_NoVoucher==value)return;
                _NoVoucher = value;
                RaisePropertyChanged("NoVoucher");
            }
        }

        [DataMember]
        public DateTime Tanggal
        {
            get { return _Tanggal; }
            set
            {
                if (Tanggal == value) return;
                _Tanggal = value;
                RaisePropertyChanged("Tanggal");
            }
        }


      



        [DataMember]
        public CoaDTO CashBankAccount
        {
            get { return _CashBankAccount; }
            set
            {
                if (ReferenceEquals(_CashBankAccount,value) || value == null)return;
                _CashBankAccount = value;
                RaisePropertyChanged("CashBankAccount");
            }
        }


        [DataMember]
        public String Description
        {
            get { return _Description; }
            set
            {
                if (value == Description) return;
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        [DataMember]
        public ObservableCollection<CashBankOutDetailDTO> CashBankOutDetails
        {
            get { return _CashBankOutDetails; }
            set
            {
                if (value == null || ReferenceEquals(value, CashBankOutDetails)) return;
                _CashBankOutDetails = value;
                RaisePropertyChanged("CashBankOutDetails");
                RefreshNumber();
            }
        }

        public int No
        {
            get { return _No; }
            set
            {
                if (_No == value)return;
                _No = value;
                RaisePropertyChanged("No");
            }
        }

        public Decimal TotalDebit
        {
            get { return _TotalDebit; }
            set
            {
                _TotalDebit = value;
                RaisePropertyChanged("TotalDebit");
            }
        }

        public Decimal TotalKredit
        {
            get { return _TotalKredit; }
            set
            {
                _TotalKredit = value;
                RaisePropertyChanged("TotalKredit");
            }
        }

        public Decimal Balance
        {
            get { return _Balance; }
            set
            {
                _Balance = value;
                RaisePropertyChanged("Balance");
            }
        }

        private void RefreshNumber()
        {
            CashBankOutDetails.CollectionChanged += CashBankInDetails_CollectionChanged;
            int number = 0;
            foreach (var detailDto in CashBankOutDetails)
            {
                number += 1;
                detailDto.No = number;
            }
        }

        private void CashBankInDetails_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            int number = 0;
            foreach (var detailDto in CashBankOutDetails)
            {
                number += 1;
                detailDto.No = number;
            }
        }
     

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        protected void RaisePropertyChanged(string propertyname)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyname));
        }
    }
}